<?php

include_once 'Classes/PHPCodeProvider.php';

/*
 * Create an instance of the CodeGeneratorOptions class
 * using the default options for code generation.
 */

$options = new CodeGeneratorOptions();

/*
 * Create an instance of the IndentedTextWriter.
 * After the generateCodeFromStatement call
 * $writer will hold the generated code.
 */

$writer = new IndentedTextWriter();

/*
 * Create an instance of the PHPCodeProvider class
 */

$provider = new PHPCodeProvider();

/*
 * Variable and field references that will be used
 * to generate if statements
 */

$thisReference = new ThisReferenceExpression();
$isWinning = new VariableReferenceExpression('isWinning');
$attemptNumber = new FieldReferenceExpression('attemptNumber', $thisReference);
$gameFinished = new FieldReferenceExpression('gameFinished', $thisReference);

/*
 * A simple if statement
 */

$ifElseStatement1 = new IfElseStatement($isWinning);

$ifElseStatement1->TrueStatements->add(
    new AssignStatement(
        new FieldReferenceExpression('isWinning', $thisReference), 
        $isWinning));


/*
 * Generate the code and output the result.
 */

$provider->generateCodeFromStatement($ifElseStatement1, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

$writer->clear();

/*
 * An if statement with both the if and the else block
 */

$ifElseStatement2 = new IfElseStatement(
    new BinaryOperatorExpression(
        BinaryOperator::LESS_THAN, 
        $attemptNumber, 
        new PrimitiveExpression(8)));

$ifElseStatement2->TrueStatements->add(
    new AssignStatement(
        $attemptNumber, 
        new PrimitiveExpression(1), 
        CompoundAssignmentOperator::PLUS_ASSIGN));

$ifElseStatement2->FalseStatements->add(
    new AssignStatement(
        $gameFinished, 
        new PrimitiveExpression(true)));

/*
 * Generate the code and output the result.
 */

$provider->generateCodeFromStatement($ifElseStatement2, $writer, $options);

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

/*
 * The output should be:
 * 
 * if($isWinning)
 * {
 *     $this->isWinning = $isWinning;
 * }
 *
 *
 * if($this->attemptNumber < 8)
 * {
 *     $this->attemptNumber += 1;
 * }
 * else
 * {
 *     $this->gameFinished = true;
 * }
 * 
 */

?>
